package ru.ifmo.kis;

import ru.ifmo.kis.domain.Material;
import ru.ifmo.kis.domain.Product;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ru.ifmo.kis.domain.Order;
import ru.ifmo.kis.domain.OrderedProduct;
import ru.ifmo.kis.domain.User;

/**
 *
 * @author ivan
 */
public class DataContext {

    Connection connection;

    public DataContext() {
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ItmoKis", "ivan", "password");
        } catch (SQLException ex) {
            connection = null;
        }
    }

    public int addOrder(User customer, int deadline, int approved) {
        String query = "INSERT INTO Orders (customer, status, deadline, approved, finished)"
                + "VALUES (?, 1, ?, ?, 0);";
        try {
            PreparedStatement st = connection.prepareStatement(query);
            st.setInt(1, customer.getId());
            st.setInt(2, deadline);
            st.setInt(3, approved);
            ResultSet res = st.executeQuery();
            return res.getInt(1);
        } catch (SQLException ex) {
            return -1;
        }
    }

    public void addOrderedProduct(Order order, Product product) {
         String query = "INSERT INTO OrderedProducts VALUES ?, ?;";
         try {
            PreparedStatement st = connection.prepareStatement(query);
            st.setInt(1, order.getId());
            st.setInt(2, product.getId());
            st.executeQuery();
         } catch (SQLException ex) {
        }
    }

    public List<OrderedProduct> getOrderedProducts(int from) {
        String query = "SELECT orderedProduct FROM ProductionQueue WHERE `time` >= ?;";
        try {
            PreparedStatement st = connection.prepareStatement(query);
            st.setInt(1, from);
            ResultSet res = st.executeQuery();

            List<OrderedProduct> ops = new ArrayList<OrderedProduct>();
            while (res.next()) {
                OrderedProduct op = new OrderedProduct(null, null);
            }

            return null;
        } catch (SQLException ex) {
            return null;
        }
    }

    public Order getOrderById(int id) {
        String query = "SELECT * FROM Orders WHERE id = ?;";
        try {
            PreparedStatement st = connection.prepareStatement(query);
            st.setInt(1, id);
            ResultSet res = st.executeQuery();


           if (res.first()) {
                return new Order();
            }

            return null;
        } catch (SQLException ex) {
            return null;
        }
    }

//    public Product getProductByName(String name) {
//        String query = "SELECT * FROM Products WHERE `name` = ?;";
//        try {
//            PreparedStatement st = connection.prepareStatement(query);
//            st.setString(1, name);
//            ResultSet res = st.executeQuery();
//            if (res.first()) {
//                return new Product(res.getInt("id"), res.getString("name"),
//                        res.getInt("duration"), getRecipeForProduct(res.getInt("id")));
//            }
//            return null;
//        } catch (SQLException ex) {
//            return null;
//        }
//    }

//    public Map<Material, Integer> getRecipeForProduct(int productId) {
//        String query = "SELECT Materials.name, Materials.volume, Recipes.`count` "
//                + "FROM Recipes INNER JOIN Materials ON Recipes.material = Materials.id "
//                + "WHERE product = ?;";
//        try {
//            PreparedStatement st = connection.prepareStatement(query);
//            st.setInt(1, productId);
//            ResultSet res = st.executeQuery();
//            Map<Material, Integer> materials = new HashMap<Material, Integer>();
//            while (res.next()) {
//                Material m = new Material(res.getString("Materials.name"), res.getDouble("Materials.volume"));
//                materials.put(m, res.getInt("Recipes.count"));
//            }
//            return materials;
//        } catch (SQLException ex) {
//            return null;
//        }
//    }
}
